// 在前端项目的 utils/request.js（或类似文件）中配置：
import axios from 'axios';

const request = axios.create({
  baseURL: 'http://localhost:8000', // 后端实际地址
  timeout: 5000,
});

// 请求拦截：自动携带本地 token
request.interceptors.request.use((config) => {
  try {
    const lsToken = typeof window !== 'undefined' ? window.localStorage.getItem('token') : null;
    const ssToken = typeof window !== 'undefined' ? window.sessionStorage.getItem('token') : null;
    const token = lsToken || ssToken;
    if (token) {
      config.headers = config.headers || {};
      config.headers.Authorization = `Bearer ${token}`;
    }
  } catch {}
  return config;
});

export default request;